﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Newinfosoft.Windows.Controls.Tools;
using System.ComponentModel;
using System.IO;
using System.Drawing;
using Microsoft.Win32;

namespace SmartMD.Manager.Common
{
    public class FilePicker : Control
    {
        static FilePicker()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(FilePicker),
                new FrameworkPropertyMetadata(typeof(FilePicker)));
        }

        public FilePicker()
        {
            FSInfo = new FilePickerInfo();
        }

        /// <summary>
        /// Identifies the ValueChanged routed event.
        /// </summary>
        public static readonly RoutedEvent FullPathChangedEvent = EventManager.RegisterRoutedEvent(
            "SelectChanged", RoutingStrategy.Bubble,
            typeof(RoutedPropertyChangedEventHandler<String>), typeof(FilePicker));

        /// <summary>
        /// Occurs when the Value property changes.
        /// </summary>
        public event RoutedPropertyChangedEventHandler<String> FullPathChanged
        {
            add { AddHandler(FullPathChangedEvent, value); }
            remove { RemoveHandler(FullPathChangedEvent, value); }
        }


        public FilePickerInfo FSInfo
        {
            get { return (FilePickerInfo)GetValue(FSInfoProperty); }
            set { SetValue(FSInfoProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FSInfo.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FSInfoProperty =
            DependencyProperty.Register("FSInfo", typeof(FilePickerInfo),
            typeof(FilePicker), new UIPropertyMetadata(null));

        public String FullPath
        {
            get { return (String)GetValue(FullPathProperty); }
            set { SetValue(FullPathProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FullPath.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FullPathProperty =
            DependencyProperty.Register("FullPath", typeof(String), typeof(FilePicker),
            new UIPropertyMetadata("请选择文件", new PropertyChangedCallback(OnFullPathChanged)));

        protected static void OnFullPathChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            FilePicker picker = d as FilePicker;

            String oldFullPath = e.OldValue as String;
            String fullPath = e.NewValue as String;

            picker.FSInfo.FullPath = fullPath;

            RoutedPropertyChangedEventArgs<String> ev = new RoutedPropertyChangedEventArgs<String>(
                oldFullPath, fullPath, FullPathChangedEvent);

            picker.RaiseEvent(ev);

        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            TextBlock tb = this.Template.FindName("tb", this) as TextBlock;
            tb.MouseLeftButtonUp += (sender, e) =>
            {
                FileDialog fd;

                fd = new OpenFileDialog();

                if (fd.ShowDialog() == true)
                {
                    FullPath = fd.FileName;
                }

            };
        }

    }

    public class FilePickerInfo : INotifyPropertyChanged
    {
        public FilePickerInfo()
        {
            Icon = IconTools.GetPixel3232Icon("__Folder");
            FullPath = "单击选择文件...";
        }

        public FilePickerInfo(String fullpath)
        {
            FullPath = fullpath;
        }

        private String m_FullPath;
        public String FullPath
        {
            get
            {
                return m_FullPath;
            }
            set
            {
                if (value != m_FullPath)
                {
                    m_FullPath = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("FullPath"));
                    }

                    FileInfo fi = new FileInfo(FullPath);
                    Icon = IconTools.GetPixel3232Icon(fi.Extension);
                }
            }
        }


        private Icon m_Icon;
        public Icon Icon
        {
            get
            {
                return m_Icon;
            }
            set
            {
                if (value != m_Icon)
                {
                    m_Icon = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Icon"));
                    }
                }
            }
        }



        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
